import { tap } from 'rxjs/operators';
import { CommonUtil } from 'src/app/common/utils/common.util';
import { ActivatedRoute } from '@angular/router';
import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { TypeService } from './service/type.service';
import { element } from '@angular/core/src/render3/instructions';

@Component({
    selector: 'app-estate-type',
    templateUrl: './estate-type.component.html',
    styleUrls: ['./estate-type.component.scss']
})
export class EstateTypeComponent implements OnInit {

    public itemsViewModal: any;
    public BaseData: any;
    public projectID: any;
    public loading: boolean = true;
    public error: boolean = false;
    tabs = []
    types = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

    constructor(
        private location: Location,
        private routeInfo: ActivatedRoute,
        private service: TypeService,
        private utils: CommonUtil,
    ) { }

    ngOnInit() {
        this.projectID = this.routeInfo.snapshot.queryParams['project'];
        this.queryLayoutSummary(this.projectID);
    }

    onClickLeft() {
        this.location.back()
    }

    onTabChange(item) {

        let str = item['name'] === "全部" ? "" : item['name']
        let _items = [];

        this.BaseData.forEach(element => {

            let name = element.Group;

            if (str === name) {
                _items.push(element);
            } else if (str === "") {
                _items.push(element);
            }

        });

        this.itemsViewModal = _items;
    }

    searchLabel(str) {

        let _items = [];

        this.BaseData.forEach(element => {
            if (str === element.Status) {
                _items.push(element);
            }
        });

        this.itemsViewModal = _items;
    }


    //api
    queryLayoutSummary(id) {

        let req = {
            "pager": { "Start": 0, "Limit": 0 },
            "filter": {
                "Lambda": "item=>item.Project==" + id,
                "Navigations": [],
                "Latitude": 28.692499,
                "Longitude": 115.815292
            },
            "sorters": [
                {
                    "Property": "item=>item.Id",
                    "Direction": "Descending"
                }]
        }

        this.service.queryLayoutSummary(req, (res) => {

            this.BaseData = res.Result.Value;
            this.itemsViewModal = res.Result.Value;

            if (this.BaseData.lenght != 0) {

                let _tabs = [];
                this.itemsViewModal.forEach(element => {

                    let name = JSON.parse(element.Group)[0];
                    element.Group = name;

                    _tabs.push(name);

                    //photo
                    element.Photo = this.utils.getThumbnail(element.Photo);

                });
                _tabs.unshift("全部");

                this.tabs = this.uniq(_tabs);
            }

            this.error = false;
            this.loading = false;

        }, error => {

            this.error = true;
            this.loading = false
        });
    }

    handleReload() {
        this.error = false
        this.loading = true
        this.queryLayoutSummary(this.projectID);
    }

    uniq(array) {
        var temp = []; //一个新的临时数组
        for (var i = 0; i < array.length; i++) {
            if (temp.indexOf(array[i]) == -1) {
                temp.push(array[i]);
            }
        }
        return temp;
    }


}
